<template>
  <div class="auth-container" v-loading="loading">
    <div class="auth-card relative">
      <Logo class="absolute top-6 left-6" size="small" />

      <div class="auth-header mt-3">
        <h1 class="auth-title">欢迎登录</h1>
        <p class="auth-subtitle">登录您的账号，开始使用全部功能</p>
      </div>

      <form @submit.prevent="handleLogin" class="auth-form">
        <div class="form-group">
          <label for="userAccount" class="form-label">用户名</label>
          <div class="input-wrapper">
            <span class="input-icon">
              <div class="i-carbon-user text-lg"></div>
            </span>
            <input
              id="userAccount"
              v-model="form.userAccount"
              class="form-input pl-10"
              placeholder="请输入用户名"
              maxlength="50"
              required
            />
          </div>
        </div>

        <div class="form-group">
          <label for="password" class="form-label">密码</label>
          <div class="input-wrapper">
            <span class="input-icon">
              <div class="i-carbon-password text-lg"></div>
            </span>
            <input
              id="password"
              type="password"
              v-model="form.password"
              class="form-input pl-10"
              placeholder="请输入密码"
              maxlength="50"
              required
            />
          </div>
        </div>

        <!-- <div class="form-options">
          <span href="#" class="forgot-password">忘记密码?</span>
        </div> -->

        <button
          type="submit"
          class="auth-btn-primary cursor-pointer"
          :disabled="loading"
          :class="{ 'opacity-60 cursor-not-allowed': loading }"
        >
          <span v-if="loading" class="i-carbon-renew animate-spin mr-2"></span>
          {{ loading ? '登录中...' : '登录' }}
        </button>

        <!-- <div class="auth-divider">
          <span>或</span>
        </div>

        <div class="social-login">
          <button type="button" class="social-btn">
            <div class="i-carbon-logo-wechat text-xl"></div>
            <span>微信登录</span>
          </button>
        </div> -->
      </form>

      <div class="auth-footer">
        <p>还没有账号？<NuxtLink to="/register" class="auth-link">立即注册</NuxtLink></p>
      </div>
    </div>
    <div class="auth-decoration">
      <div class="auth-decoration-circle circle-1"></div>
      <div class="auth-decoration-circle circle-2"></div>
      <div class="auth-decoration-circle circle-3"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import { queryLogin } from '~/apis/auth';
import { ElMessage } from 'element-plus';

const userStore = useUserStore();
const loading = ref(false);
const form = reactive({ userAccount: '', password: '' });
const rememberMe = ref(false);

const handleLogin = async () => {
  loading.value = true;
  const res = await queryLogin(form).finally(() => (loading.value = false));
  loading.value = true;
  await userStore.onLogin(res.data).finally(() => (loading.value = false));
  navigateTo('/');
};
definePageMeta({ layout: 'auth' });

onMounted(() => {
  if (userStore.isLoggedIn) navigateTo('/');
});
</script>

<style scoped>
.auth-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: linear-gradient(135deg, #f5f3ff 0%, #e0e7ff 100%);
  position: relative;
  overflow: hidden;
}

.auth-card {
  background: white;
  width: 100%;
  max-width: 420px;
  border-radius: 1.5rem;
  box-shadow: 0 10px 25px rgba(107, 70, 193, 0.1);
  padding: 2.5rem;
  position: relative;
  z-index: 10;
}

.auth-header {
  text-align: center;
  margin-bottom: 2rem;
}

.auth-title {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--primary-purple, #6b46c1);
  margin-bottom: 0.5rem;
}

.auth-subtitle {
  color: var(--text-secondary, #64748b);
  font-size: 0.95rem;
}

.auth-form {
  margin-bottom: 1.5rem;
}

.form-group {
  margin-bottom: 1.25rem;
}

.form-label {
  display: block;
  font-weight: 500;
  margin-bottom: 0.5rem;
  color: var(--text-primary, #1e293b);
  font-size: 0.9rem;
}

.input-wrapper {
  position: relative;
}

.input-icon {
  position: absolute;
  left: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  color: #94a3b8;
}

.form-input {
  display: block;
  width: 100%;
  padding: 0.75rem 1rem;
  font-size: 1rem;
  line-height: 1.5;
  color: #1e293b;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid #e2e8f0;
  border-radius: 0.75rem;
  transition:
    border-color 0.15s ease-in-out,
    box-shadow 0.15s ease-in-out;
}

.form-input:focus {
  border-color: #a78bfa;
  box-shadow: 0 0 0 3px rgba(167, 139, 250, 0.2);
  outline: none;
}

.form-options {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  margin-bottom: 1.5rem;
}

.remember-me {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: #64748b;
  font-size: 0.875rem;
  cursor: pointer;
}

.forgot-password {
  color: var(--primary-purple, #6b46c1);
  font-size: 0.875rem;
  transition: color 0.2s;
  text-decoration: none;
}

.forgot-password:hover {
  color: var(--secondary-purple, #805ad5);
  text-decoration: underline;
}

.auth-btn-primary {
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: center;
  background-color: var(--primary-purple, #6b46c1);
  color: white;
  font-weight: 600;
  padding: 0.75rem;
  border-radius: 9999px;
  transition: all 0.2s;
  box-shadow: 0 4px 6px rgba(107, 70, 193, 0.2);
  margin-bottom: 1.5rem;
}

.auth-btn-primary:hover:not(:disabled) {
  background-color: var(--secondary-purple, #805ad5);
  transform: translateY(-1px);
  box-shadow: 0 6px 8px rgba(107, 70, 193, 0.3);
}

.auth-divider {
  display: flex;
  align-items: center;
  margin: 1.5rem 0;
}

.auth-divider::before,
.auth-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background-color: #e2e8f0;
}

.auth-divider span {
  padding: 0 1rem;
  color: #94a3b8;
  font-size: 0.875rem;
}

.social-login {
  margin-bottom: 1.5rem;
}

.social-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 0.75rem;
  border-radius: 9999px;
  border: 1px solid #e2e8f0;
  background-color: white;
  color: #1e293b;
  transition: all 0.2s;
  gap: 0.5rem;
}

.social-btn:hover {
  background-color: #f8fafc;
  border-color: #cbd5e1;
}

.auth-footer {
  text-align: center;
  color: #64748b;
  font-size: 0.875rem;
}

.auth-link {
  color: var(--primary-purple, #6b46c1);
  font-weight: 500;
  text-decoration: none;
  transition: color 0.2s;
}

.auth-link:hover {
  color: var(--secondary-purple, #805ad5);
  text-decoration: underline;
}

.auth-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.auth-decoration-circle {
  position: absolute;
  border-radius: 50%;
  background: linear-gradient(135deg, #9f7aea 0%, #7f9cf5 100%);
  opacity: 0.1;
  animation-duration: 15s;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
}

.circle-1 {
  width: 300px;
  height: 300px;
  top: -100px;
  right: -50px;
  animation-name: float-1;
}

.circle-2 {
  width: 200px;
  height: 200px;
  bottom: -50px;
  left: -50px;
  animation-name: float-2;
}

.circle-3 {
  width: 100px;
  height: 100px;
  top: 40%;
  right: 10%;
  animation-name: float-3;
}

@keyframes float-1 {
  0%,
  100% {
    transform: translate(0, 0) rotate(0deg);
  }
  50% {
    transform: translate(-30px, 30px) rotate(5deg);
  }
}

@keyframes float-2 {
  0%,
  100% {
    transform: translate(0, 0) rotate(0deg);
  }
  50% {
    transform: translate(30px, -20px) rotate(-5deg);
  }
}

@keyframes float-3 {
  0%,
  100% {
    transform: translate(0, 0) rotate(0deg);
  }
  50% {
    transform: translate(-20px, -30px) rotate(3deg);
  }
}

.animate-spin {
  animation: spin 1s linear infinite;
}

.logo-container {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.logo-container:hover {
  transform: scale(1.05);
}

.logo-image {
  width: 28px;
  height: 28px;
}

.logo-text {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--primary-purple, #6b46c1);
}
</style>
